package com.ccc.boot.system.controller;

import com.ccc.boot.base.BaseController;
import com.ccc.boot.constant.CommonConstant;
import com.ccc.boot.exception.BusinessException;
import com.ccc.boot.response.ResponseResult;
import com.ccc.boot.system.entity.SysRole;
import com.ccc.boot.system.pojo.AuthorizeApiParam;
import com.ccc.boot.system.pojo.AuthorizeMenuParam;
import com.ccc.boot.system.pojo.RelateUserParam;
import com.ccc.boot.system.service.RelUserRoleService;
import com.ccc.boot.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 接口资源表 REST接口
 *
 * @author superC
 * @version 0.1.0
 * @date 2022-03-30
 */

@RestController
@RequestMapping(CommonConstant.SYS_URI_PREFIX + "/role")
public class SysRoleController extends BaseController<SysRole, SysRoleService> {

    @Autowired
    private RelUserRoleService relUserRoleService;

    /**
     * 关联用户
     *
     * @param param
     * @return {@link ResponseResult}
     * @date 2022/3/31 15:28
     * @author superC
     */
    @PostMapping("/relateUsers")
    public ResponseResult relateUsers(@RequestBody RelateUserParam param) {
        return relUserRoleService.relateUsers(param);
    }

    /**
     * 取消关联多个用户
     *
     * @param param
     * @return {@link ResponseResult}
     * @date 2022/3/31 15:28
     * @author superC
     */
    @PostMapping("/unRelateUsers")
    public ResponseResult unRelateUsers(@RequestBody RelateUserParam param) {
        return relUserRoleService.deleteUsers(param);
    }

    /**
     * 取消关联用户
     *
     * @param roleId
     * @param userId
     * @return {@link ResponseResult}
     * @date 2022/3/31 15:28
     * @author superC
     */
    @DeleteMapping("/unRelateUser/{roleId}/{userId}")
    public ResponseResult unRelateUser(@PathVariable Long roleId, @PathVariable Long userId) {
        try {
            boolean result = relUserRoleService.deleteUser(userId, roleId);
            if (result) {
                return ResponseResult.success("取消关联用户成功");
            }
        } catch (Exception e) {
            logger.error("【捕获异常-取消关联用户操作】\r\n异常记录：", e);
            throw new BusinessException(e.getMessage());
        }
        return ResponseResult.failure("取消关联用户失败");
    }

    /**
     * 授权菜单资源
     *
     * @param param
     * @return {@link ResponseResult}
     * @date 2022/4/18 13:14
     * @author superC
     */
    @PostMapping("/authorizeMenu")
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult authorizeMenu(@RequestBody AuthorizeMenuParam param) {
        try {
            boolean result = service.authorizeMenu(param);
            if (result) {
                return ResponseResult.success("授权成功");
            }
        } catch (Exception e) {
            logger.error("【捕获异常-授权菜单资源操作】\r\n异常记录：", e);
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BusinessException(e.getMessage());
        }
        return ResponseResult.failure("授权失败");
    }

    /**
     * 授权按钮资源
     *
     * @param param
     * @return {@link ResponseResult}
     * @date 2022/4/18 13:14
     * @author superC
     */
    @PostMapping("/authorizeApi")
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult authorizeApi(@RequestBody AuthorizeApiParam param) {
        try {
            boolean result = service.authorizeApi(param);
            if (result) {
                return ResponseResult.success("授权成功");
            }
        } catch (Exception e) {
            logger.error("【捕获异常-授权按钮资源操作】\r\n异常记录：", e);
            //手工回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new BusinessException(e.getMessage());
        }
        return ResponseResult.failure("授权失败");
    }

}
